package com.ch.d0312.d0316.biz;

import com.ch.util.DBHelper;

import java.sql.Date;

public class BookBiz {

    public void create(String bookname, String bookauthor,
                       String bookpress, String pressdate, String bookimage) throws BizException {

        check(bookname,bookauthor,bookpress,pressdate,bookimage);


        String sql = "insert into bm_book (bookname,bookauthor,bookpress,pressdate)" +
                "value (?,?,?,?)";

        DBHelper.update(sql,bookname,bookauthor,bookpress,
                Date.valueOf(pressdate));
    }


    public void modify(String id, String bookname, String bookauthor,
                       String bookpress, String pressdate, String bookimage) throws BizException {
        check(bookname,bookauthor,bookpress,pressdate,bookimage);

        String sql = "update bm_book set bookname=?,bookauthor=?,bookpress=?,pressdate=?,bookimage=?" + "where id =?";

        DBHelper.update(sql,bookname,bookauthor,bookpress,Date.valueOf(pressdate),bookimage,id);
    }

    public void check(String bookname, String bookauthor,
                      String bookpress, String pressdate, String bookimage) throws BizException {
        if(bookname==null || bookname.trim().isEmpty()){
            throw new BizException("请输入书名");

        }
        if(bookpress.length() < 3){
            throw new BizException("出版社至少是3个字符");
        }

        if (pressdate.matches("\\d{4}--\\d{1,2}--\\d{1,2}") == false){
            throw new BizException("出版日期格式不正确");
        }
    }

    // 物理删除
    public void remove(String id){
        // 删除前要判断，该数据是否被其他表引用
        String sql = "delete from bm_book where id=?";
        DBHelper.update(sql,id);
    }

    // 逻辑删除，新增状态字段: status = 1 正常， status = 0 删除
    public void remove1(String id){
        // 删除前要判断，该数据是否被其他表引用
        String sql = "update bm_book set status = 0 where id=?";
        DBHelper.update(sql,id);
    }


}
